package yl.hs.bmipfull.controllers.system;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IMenuButtonService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:  菜单按钮控制层<br/>
 * 创建时间: 2021年05月12日 10时15分31秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "菜单按钮Web接口", tags = "系统管理——菜单按钮操作")
@RequestMapping(value = "/api/menu")
@RestController
public class MenuButtonController extends ControllerBase {

    @Autowired
    IMenuButtonService service;

    @ApiOperation(value = "查询菜单按钮", notes = "查询菜单按钮")
    @RequestMapping(value = "/button", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询菜单按钮", notes = "根据菜单按钮编号查询菜单按钮")
    @RequestMapping(value = "/button/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@PathVariable String sn) {

        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存菜单按钮", notes = "查询/更新菜单按钮信息")
    @RequestMapping(value = "/button", method = RequestMethod.PUT)
    public WebResult saveMenu(@RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveMenuButton(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }


    @ApiOperation(value = "查询菜单按钮", notes = "根据菜单编号查询菜单按钮信息")
    @RequestMapping(value = "/buttons/{menuSN}", method = RequestMethod.GET)
    public WebResult queryByMenuSn(@PathVariable String menuSN) {
        return success("查询成功", service.queryByMenuSn(menuSN));
    }

    @ApiOperation(value = "删除系统菜单按钮信息", notes = "根据系统菜单编号删除系统菜单按钮信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    @ResponseBody
    public WebResult delete(@ApiParam(value = "系统菜单按钮编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}